<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>YUI Event Delegate Tests</title>

	<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/logger/assets/logger.css">
	<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/yuitest/assets/testlogger.css">

	<script type="text/javascript" src="../../../build/yahoo/yahoo-min.js"></script>
	<script type="text/javascript" src="../../../build/dom/dom-min.js"></script>
	<script type="text/javascript" src="../../../build/event/event-min.js"></script>	
	<script type="text/javascript" src="../../../build/event-simulate/event-simulate-min.js"></script>	
	<script type="text/javascript" src="../../../build/logger/logger-min.js"></script>
	<script type="text/javascript" src="../../../build/yuitest/yuitest-min.js"></script>

	<script type="text/javascript">

		(function () {
			
			var Event = YAHOO.util.Event,
				Dom = YAHOO.util.Dom,
				Assert = YAHOO.util.Assert,
				UserAction = YAHOO.util.UserAction;
			
			Event.onDOMReady(function () {

				var testLogger = new YAHOO.tool.TestLogger();

			    var focusTest = new YAHOO.tool.TestCase({

			        name: "Event Delegation Test",

			        test_add_focusin : function () {

						var foo = false, 
							target,
							boundEl,
							bRemoved,

							onFocus = function(e) {

								foo = true;
								boundEl = this;
								target = Event.getTarget(e);

							};
						
						Event.on('container', 'focusin', onFocus);

						Dom.get('button-1').focus();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						Assert.areEqual(target, Dom.get('button-1'), "the target is the incorrect node, should be the actual focus target");
						Assert.areEqual(boundEl, Dom.get('container'), "the default scope should be the bound element");

						foo = false;
						target = null;
						boundEl = null;

						Dom.get('text-1').focus();
						Dom.get('button-1').focus();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						Assert.areEqual(target, Dom.get('button-1'), "the target is the incorrect node, should be the actual focus target");
						Assert.areEqual(boundEl, Dom.get('container'), "the default scope should be the bound element");
						
						Dom.get('button-1').blur();

			        },
			
					test_remove_focusin: function () {

						var foo = false,

							onFocus = function(e) {

								foo = true;

							};
						
						Event.on('container', 'focusin', onFocus);

						Dom.get('button-1').focus();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						
						bRemoved = Event.removeListener('container', 'focusin', onFocus);

						Assert.isTrue(bRemoved, "Removal of focusin listener event failed");
						
						Dom.get('button-1').blur();						
						
						foo = false;
						
						Dom.get('button-1').focus();

						Assert.isFalse(foo, "container should not pickup the focus event after listener is removed");
						
						Dom.get('button-1').blur();
				
					},
					
					test_purge_focusin: function () {

						var foo = false,

							onFocus = function(e) {

								foo = true;

							};
						
						Event.on('container', 'focusin', onFocus);

						Dom.get('button-1').focus();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						
						Event.purgeElement('container', false, 'focusin');
						
						Dom.get('button-1').blur();

						foo = false;
						
						Dom.get('button-1').focus();

						Assert.isFalse(foo, "container should not pickup the focus event after listener has been purged");
						
					},
					
			        test_add_focusout : function () {

						var foo = false, 
							target,
							boundEl,
							bRemoved,

							onBlur = function(e) {

								foo = true;
								boundEl = this;
								target = Event.getTarget(e);

							};
						
						Event.on('container', 'focusout', onBlur);

						Dom.get('button-1').focus();
						Dom.get('button-1').blur();

						Assert.isTrue(foo, "simple focusout fails, container should pickup the focus event");
						Assert.areEqual(target, Dom.get('button-1'), "the target is the incorrect node, should be the actual blur target");
						Assert.areEqual(boundEl, Dom.get('container'), "the default scope should be the bound element");
						
						foo = false;
						target = null;
						boundEl = null;
						
						Dom.get('button-1').focus();
						Dom.get('text-1').focus();
						
						Assert.isTrue(foo, "simple focusout fails, container should pickup the focus event");
						Assert.areEqual(target, Dom.get('button-1'), "the target is the incorrect node, should be the actual blur target");
						Assert.areEqual(boundEl, Dom.get('container'), "the default scope should be the bound element");						

			        },
			
					test_remove_focusout: function () {

						var foo = false, 
							bRemoved,

							onBlur = function(e) {

								foo = true;

							};
						
						Event.on('container', 'focusout', onBlur);

						Dom.get('button-1').focus();
						Dom.get('button-1').blur();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						
						foo = false;

						bRemoved = Event.removeListener('container', 'focusout', onBlur);
						
						Dom.get('button-1').focus();
						Dom.get('text-1').focus();
						
						Assert.isTrue(bRemoved, "Removal of focusout listener event failed");
						Assert.isFalse(foo, "container should not pickup the focusout event after listener has been removed");
				
					},				

					test_purge_focusout: function () {

						var foo = false,

							onBlur = function(e) {

								foo = true;

							};
						
						Event.on('container', 'focusout', onBlur);

						Dom.get('button-1').focus();
						Dom.get('button-1').blur();

						Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
						
						foo = false;

						Event.purgeElement('container', false, 'focusout');
						
						Dom.get('button-1').focus();
						Dom.get('text-1').focus();
						
						Assert.isFalse(foo, "container should not pickup the focusout event after listener has been purged");
				
					}
			
			    });


	            YAHOO.tool.TestRunner.add(focusTest);

	            if (parent && parent != window) {
	                YAHOO.tool.TestManager.load();
	            } else {
	                YAHOO.tool.TestRunner.run();
	            }

			});
			
		}());
	
	</script>

</head>
<body class="yui-skin-sam">

	<div id="container">
		<button id="button-1">Click Me!</button>
		<a id="anchor-1" href="http://www.yahoo.com">Click Me!</a>
		<input type="text" id="text-1">
	</div>

</body>
</html>
`